package cn.itcast.documentapis;

import org.elasticsearch.action.ActionListener;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.reindex.BulkByScrollResponse;
import org.elasticsearch.index.reindex.DeleteByQueryAction;
import org.junit.jupiter.api.Test;

/**
 * @Description TODO : 通过查询API删除允许人们根据查询结果删除给定的文档集：
 * @Author: zj
 * @Date: 10:28 2019/3/8
 * @Version 1.0
 **/
public class DeleteByQueryAPI {

    @Test
    public void deleteApi() {
        TransportClient client = esClient.getClient();
        //删除索引bank下 account_number=20的数据  异步读取
        DeleteByQueryAction.INSTANCE.newRequestBuilder(client)
                .filter(QueryBuilders.matchQuery("account_number", "20"))
                .source("bank")
                .execute(new ActionListener<BulkByScrollResponse>() {
                    public void onResponse(BulkByScrollResponse bulkByScrollResponse) {
                        long deleted = bulkByScrollResponse.getDeleted();
                        System.out.println(deleted);
                    }
                    public void onFailure(Exception e) {
                    }
                });



//        long deleted = bulkByScrollResponse.getDeleted();
//        System.out.println(deleted);
    }
}
